//按下回车触发登录事件
document.onkeydown = function(event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 13) { // enter 键
            login.doLogin();
        }
    }
    //点击登录触发事件
$("#login").click(function() {
    login.doLogin();
});
// 表单的输入提示
$("#username").blur(function() {
    if ($(this).val().length < 6) {
        layer.tips("用户名不能少于6位", "#username");
    }
});
$("#password").blur(function() {
    if ($(this).val().length < 6) {
        layer.tips("密码不能少于6位", "#password");
    }
});

// 登录方法
var login = {
    flog: false,
    doLogin: function() {
        //登录流程验证
        login.username = $("#username").val();
        login.password = $("#password").val();
        this.check();
        if (login.flog) {
            $.post(
                "/index.php/Login/login", {
                    username: login.username,
                    password: login.password
                },
                function(reg) {
                    if (reg.status == "success") {
                        layer.msg(reg.data, {
                            icon: 1
                        })
                        setTimeout(function() {
                            window.location.href = "/";
                        }, 1500);
                    } else if (reg.status == "failed") {
                        layer.msg(reg.data, {
                            icon: 2
                        })
                    }
                }
            );
        }
    },
    check: function() {
        //提交给服务器前的验证
        if (login.username.length < 6) {
            layer.msg("用户名长度不能少于6位", {
                icon: 2
            })
            $("#username").focus();
        } else if (login.password.length < 6) {
            layer.msg("密码长度必须大于6位", {
                icon: 2
            })
            $("#password").focus();
        } else {
            login.flog = true;
        }
    }
};
